package server

import (
	"context"
	v1 "goGameWebServer/api/server/v1"
	"goGameWebServer/internal/controller"
	"goGameWebServer/internal/defines/version"
	"goGameWebServer/internal/manager/databasemanager"
	"goGameWebServer/internal/model/entity"

	"github.com/gogf/gf/v2/frame/g"
)

func (c *ServerControllerV1) ServerQuery(ctx context.Context, req *v1.ServerQueryReq) (res *v1.ServerQueryRes, err error) {
	base, err := databasemanager.ManagerDatabase().DefaultDataBase()
	if err != nil {
		return nil, err
	}
	all, err := base.GetDb().Model(entity.ServerModelData{}).Order("sort asc, id asc").All()
	if err != nil {
		return nil, err
	}
	res = &v1.ServerQueryRes{}

	if all != nil && all.Len() > 0 {
		if err = all.Structs(&res.Servers); err != nil {
			controller.BackResponseError(g.RequestFromCtx(ctx), -1, err.Error())
			return
		}
		if len(res.Servers) > 0 {
			res.Default = res.Servers[0]
		}
	}

	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}

func (c *ServerControllerV1) ServerVersion(ctx context.Context, req *v1.ServerVersionReq) (res *v1.ServerVersionRes, err error) {
	controller.BackResponseOK(g.RequestFromCtx(ctx), version.Version)
	return
}
